package com.duruikang.LeaveAPI.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.duruikang.LeaveAPI.dao.LeavingDao;
import com.duruikang.LeaveAPI.entity.Leaving;
import com.duruikang.LeaveAPI.entity.Teacher;
import com.duruikang.LeaveAPI.service.LeavingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author szh
 */
@Service
public class LeavingServiceImpl extends ServiceImpl<LeavingDao, Leaving> implements LeavingService {

    @Autowired
    private Environment environment;

    @Autowired
    private TeacherInformationServiceImpl teacherService;

    @Override
    public boolean submitLeaving(String studentId, int leavingReason, int leavingType, String leavingDestination,
                                 String leavingDetail, LocalDate leavingStart, LocalDate leavingEnd) {
        boolean res = false;
        Leaving newLeaving = new Leaving();
        newLeaving.setLeavingId(UUID.randomUUID().toString());

        String newCode;
        Map<String, String> maxCodeMap = baseMapper.getMaxLeavingCode();
        if (maxCodeMap == null) {
            newCode = "Q000000001";
        } else {
            //截掉Q，转int，+1，转8位字符串，前加Q
            String maxCode = maxCodeMap.get("maxLeavingCode");
            if (StrUtil.isEmpty(maxCode)) {
                newCode = "Q000000001";
            } else {
                newCode = "Q" + NumberUtil.decimalFormat("000000000", Integer.parseInt(maxCode.substring(1)) + 1);
            }
        }
        newLeaving.setLeavingCode(newCode);

        newLeaving.setStudentId(studentId);
        newLeaving.setLeavingReason(leavingReason);
        newLeaving.setLeavingType(leavingType);
        newLeaving.setLeavingDestination(leavingDestination);
        newLeaving.setLeavingDetail(leavingDetail);
        newLeaving.setLeavingDate(LocalDate.now());//使用当前时间
        newLeaving.setLeavingStart(leavingStart);
        newLeaving.setLeavingEnd(leavingEnd);

        String defaultStateSetting = environment.getProperty("leaving.defaultState");

        if (defaultStateSetting != null) {
            newLeaving.setLeavingState(Integer.parseInt(defaultStateSetting));
            res = save(newLeaving);
        }
        return res;
    }

    @Override
    public List<Map<String, String>> getAllLeavings(String accountId) {
        return baseMapper.getLeavings(accountId);
    }

    @Override
    public Map<String, String> getLeaving(String leavingId) {
        return baseMapper.getLeaving(leavingId);
    }

    @Override
    public List<Map<String, String>> getExaminingLeavings(String accountId) {
        Teacher teacher = teacherService.query().eq("account_id",accountId).one();
        if(teacher.getTeacherType()==31){
            return baseMapper.getExaminingLeavings2(accountId);
        }else{
            return baseMapper.getExaminingLeavings1(accountId);
        }

    }
}
